package cat.service.impl;


import cat.entity.Article;
import cat.entity.ArticleCategoryRef;
import cat.entity.Category;
import cat.mapper.ArticleCategoryRefMapper;
import cat.mapper.ArticleTagRefMapper;
import cat.mapper.CategoryMapper;
import cat.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    ArticleCategoryRefMapper articleCategoryRefMapper;

    public List<Category> listCategory() {
        return categoryMapper.listCategory();
    }

    public List<Category> listCategoryByParentId(Integer parentId) {
        return categoryMapper.listCategoryByParentId(parentId);
    }

    public List<Category> selectAllCategory() {

        List<Category> categoryList = categoryMapper.selectAllCategory();
        for (Category category : categoryList) {
            List<Article> articleList = articleCategoryRefMapper.listArticleByCategoryId(category.getCategoryId());
            category.setArticleCount(articleList.size());
        }

        return categoryList;
    }

    public void deleteCategoryById(Integer categoryId) {
        categoryMapper.deleteCategoryById(categoryId);
    }


    public void addCategory(Category category) {
        categoryMapper.addCategory(category);
    }

    public List<Category> selectCategoryById(Integer categoryId) {
        return categoryMapper.selectCategoryById(categoryId);
    }

    public void updateCategory(Category category) {
        categoryMapper.updateCategory(category);
    }
}
